package com.tech.service.storage;

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import io.seata.spring.annotation.GlobalTransactionScanner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @author litianlin
 * @date 2023/12/12 14:31
 * @description:
 */
@Configuration
public class SeataConfig {
    @Value("${dubbo.application.name}")
    private String applicationName;

    @Bean
    public GlobalTransactionScanner globalTransactionScanner(){
        GlobalTransactionScanner scanner = new GlobalTransactionScanner(applicationName, "TEST");
        return scanner;
    }

    @Bean
    @ConfigurationProperties("spring.datasource")
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource;
    }

    /**必须配置代理，否则无法管理事务*/
    @Bean
    @Primary
    public DataSource dataSourceProxy(){
        DataSourceProxy dataSourceProxy = new DataSourceProxy(dataSource());
        return dataSourceProxy;
    }
}
